package com.zik.vod.controller;


import com.atguigu.ggkt.model.vod.Teacher;
import com.atguigu.ggkt.vo.vod.TeacherQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zik.swagger.result.Result;
import com.zik.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zik
 * @since 2023-04-18
 */
@Slf4j
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有教师
     * @return list<Teacher> 教师列表
     */
    @GetMapping("findAll")
    public Result findAllTeacher() {
        List<Teacher> list = teacherService.list();
        return Result.ok(list);
    }

    /**
     * 逻辑删除
     * @return Result
     */
    @DeleteMapping("remove/{id}")
    public Result removeTeacher(@PathVariable long id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     * 条件查询
     * @param current 查询页
     * @param limit 每页多少条
     * @param teacherQueryVo 查询条件
     * @return Page<Teather>
     */
    @ApiOperation(value = "条件查询")
    @PostMapping("findQueryPage/{current}/{limit}")
    public Result findPage(@PathVariable long current , @PathVariable long limit ,@RequestBody(required = false) TeacherQueryVo teacherQueryVo) {
        log.info("current = {} , limit = {} , teacherQueryVo = {}",current,limit,teacherQueryVo);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        Page<Teacher> pageParam = new Page<>(current,limit);
        // 判断 teacherQueryVo 对象是否为空
        if (teacherQueryVo == null) {
            IPage<Teacher> page  = teacherService.page(pageParam,queryWrapper);
            return Result.ok(page);
        }else {
            String name = teacherQueryVo.getName();
            Integer level = teacherQueryVo.getLevel();
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();
            // 非空判断
            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like("name",name);
            }
            if (!StringUtils.isEmpty(level)) {
                queryWrapper.eq("level",level);
            }
            if (!StringUtils.isEmpty(joinDateBegin)) {
                queryWrapper.ge("join_date",joinDateBegin);
            }
            if (!StringUtils.isEmpty(joinDateEnd)) {
                queryWrapper.le("join_date",joinDateEnd);
            }
            // 执行分页查询
            IPage<Teacher> page = teacherService.page(pageParam, queryWrapper);
            return Result.ok(page);
        }
    }

    /**
     * 添加讲师
     * @return
     */
    @ApiOperation("添加讲师")
    @PostMapping("save")
    public Result saveTeacher(@RequestBody Teacher teacher) {
        boolean isSuccess = teacherService.save(teacher);
        if (isSuccess) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }

    }

    /**
     * 根据id查询
     * @param id teacher的id
     * @return null
     */
    @ApiOperation(("根据id查询"))
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable long id) {
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }

    /**
     * 修改讲师
     * @param teacher
     * @return null
     */
    @ApiOperation("修改功能")
    @PutMapping("updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher) {
        boolean isSuccess = teacherService.updateById(teacher);
        if (isSuccess) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     * 批量删除
     * @param idList id列表
     * @return null
     */
    @ApiOperation("批量删除")
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> idList)  {
        boolean isSuccess = teacherService.removeByIds(idList);
        if(isSuccess) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

}

